package com.ycsj.goldmedalnewconcept.common.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.mob.MobSDK;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.sina.weibo.SinaWeibo;

/**
 * 官方网站界面
 * Created by Kevin on 2019/4/23
 */
public class GuanwangActivity extends BaseActivity implements OnClickListener {

    //返回
    private ImageView iv_back;

    //分享
    private ImageView iv_share;

    private WebView webview;

    private String url;

    private String logo;

    /**
     * 调换到当前界面的方法
     *
     * @param context
     * @param url
     */
    public static void goActivity(Context context, String url) {
        Intent it = new Intent(context, GuanwangActivity.class);
        it.putExtra("url", url);
        context.startActivity(it);
    }

    @Override
    protected int getContentLayout() {

        return R.layout.activity_guanwang;
    }

    @Override
    protected void init() {

        //初始化状态栏
        initStateBar();

        initView();

        initIntent();

        setOnListener();

        initData();
    }

    /**
     * 接收数据
     */
    private void initIntent() {
        url = getIntent().getStringExtra("url");

        if (TextUtils.isEmpty(url)) {
            url = Constants.ENTER_URL;
        }

        if (TextUtils.isEmpty(AccountUtils.getLogo(this))) {
            logo = "http://139.129.57.83:8080/Appandroid/applogo.png";
        } else {
            TextUtils.isEmpty(AccountUtils.getLogo(this));
        }
    }

    /**
     * 初始化控件
     */
    private void initView() {
        iv_back = findViewById(R.id.iv_back);
        webview = findViewById(R.id.webView);
        iv_share = findViewById(R.id.iv_share);

    }

    /**
     * 初始化数据
     */
    private void initData() {
        webview = findViewById(R.id.webView);

        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setPluginState(PluginState.ON);
        settings.setAllowFileAccess(true);
        settings.setLoadWithOverviewMode(true);

        webview.loadUrl(url);
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });
    }

    private void setOnListener() {
        // TODO Auto-generated method stub
        iv_back.setOnClickListener(this);
        iv_share.setOnClickListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        webview.onPause();
        webview.pauseTimers();
    }

    @Override
    public void onResume() {
        super.onResume();
        webview.resumeTimers();
        webview.onResume();
    }

    @Override
    protected void onDestroy() {
        webview.destroy();
        webview = null;
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_share:
                share();

                break;
        }
    }

    /**
     * 分享活动
     */
    private void share() {
        // TODO Auto-generated method stub
        MobSDK.init(this, "12c24ea0b59b0",
                "aae63565fbd69b1acbfcdd0d5ac0b35c");
        OnekeyShare oks = new OnekeyShare();
        // 关闭sso授权
        oks.disableSSOWhenAuthorize();
        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {

            @Override
            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
                String title;
                if(TextUtils.isEmpty(AccountUtils.getSchoolName(GuanwangActivity.this))){
                     title = "欢迎访问英才世家官方网站";
                }else{
                    title ="欢迎访问" + AccountUtils.getSchoolName(GuanwangActivity.this)+"官方网站";
                }

                if (SinaWeibo.NAME.equals(platform.getName())) {
                    paramsToShare.setTitle(getString(R.string.share));
                    paramsToShare.setTitleUrl(url);
                    paramsToShare.setTitle(title);
                    paramsToShare.setText(title);
                    paramsToShare
                            .setImageUrl(logo);
                    paramsToShare.setUrl(url);
                    paramsToShare.setComment("");
                    paramsToShare.setSite("金牌君");
                    paramsToShare.setSiteUrl(url);
                } else {
                    paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
                    paramsToShare.setTitle(getString(R.string.share));
                    paramsToShare.setTitleUrl(url);
                    paramsToShare.setTitle(title);
                    paramsToShare.setText(title);
                    paramsToShare
                            .setImageUrl(logo);

                    paramsToShare.setUrl(url);
                    paramsToShare.setComment("");
                    paramsToShare.setSite("金牌君");
                    paramsToShare.setSiteUrl(url);
                }
            }
        });
        oks.show(this);
    }
}
